package model.booking;

import common.RoomTypes;
import model.room.Room;
import model.room.RoomCount;
import model.user.User;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 预约类
 */
public class Booking implements Serializable {
    private static List<Booking> bookingList = new ArrayList<>();
    private User user;
    private String checkInPerson;   //入住人姓名
    private String checkInPersonId;  //入住人身份证号
    private LocalDate checkInTime;   //入住时间
    private LocalDate checkOutTime;  //退房时间
    private RoomTypes roomTypes;    //房间 //更改为房间类型
    private double price;   //价格
    private String status;  //预约状态
    public Booking(){

    }
    /*
        无用构造函数
     */
//    public Booking(User user, LocalDate checkInTime, LocalDate checkOutTime, RoomTypes roomTypes, double price){
//        this.user = user;
//        this.checkInTime = checkInTime;
//        this.checkOutTime = checkOutTime;
//        this.roomTypes = roomTypes;
//        this.price = price;
//        this.status = "status";
//    }

    public Booking(User user, String checkInPerson, String checkInPersonId, LocalDate checkInTime, LocalDate checkOutTime,RoomTypes roomTypes){

        this.user = user;
        this.checkInPerson = checkInPerson;
        this.checkInPersonId = checkInPersonId;
        this.checkInTime = checkInTime;
        this.checkOutTime = checkOutTime;
        this.roomTypes = roomTypes;
        this.price = getPrice(roomTypes);
        this.status = "status";
    }

    //获取价格
    public double getPrice(RoomTypes roomTypes) {
        if(roomTypes.equals(RoomTypes.Single_ROOM)){
            return 100.0;
        } else if (roomTypes.equals(RoomTypes.Twin_ROOM)) {
            return 150.0;
        }else if (roomTypes.equals(RoomTypes.Double_ROOM)){
            return 200.0;
        }else{
            return 399.0;
        }
    }
    //酒店预约信息添加
    public static void addBooking(Booking booking) {
        bookingList.add(booking);
    }
    //更新
    public static void deleteBooking(Booking booking) {
        bookingList.remove(booking);
    }

    //判断这个room在checkInTime到checkOutTime之间预约的个数是否超过30个
    public static boolean isFull(LocalDate checkInTime, LocalDate checkOutTime, RoomTypes roomTypes) {
        int count = 0;//已预约的房间数量
        for (Booking booking : bookingList) {
            if (booking.getRoomTypes().equals(roomTypes) && booking.getCheckInTime().isBefore(checkOutTime) && booking.getCheckOutTime().isAfter(checkInTime)) {
                count++;
            }
        }
        RoomCount roomCount = new RoomCount(roomTypes,checkInTime,checkOutTime);
        if (count >= roomCount.getRoomTypesCount(roomTypes) ) {
                return true;
        } else {
            return false;
        }
    }
    //判断这个Booking有没有出现过一个属性一个属性的判断
    public static boolean isExist(Booking booking) {
        for (Booking booking1 : bookingList) {
            if (booking1.getUser().equals(booking.getUser()) && booking1.getCheckInPerson().equals(booking.getCheckInPerson()) && booking1.getCheckInPersonId().equals(booking.getCheckInPersonId()) && booking1.getCheckInTime().equals(booking.getCheckInTime()) && booking1.getCheckOutTime().equals(booking.getCheckOutTime()) && booking1.getRoomTypes().equals(booking.getRoomTypes()) && booking1.getPrice() == booking.getPrice()) {
                return true;
            }
        }
        return false;
    }
    //获取该user的预约记录
    public static List<Booking> getBookingHostryList(User user) {
        List<Booking> bookingList1 = new ArrayList<>();
        for (Booking booking : bookingList) {
            if (booking.getUser().equals(user)) {
                bookingList1.add(booking);
            }
        }
        return bookingList1;
    }
    public static void DeletBookingHostryList(User user,int row){
        List<Booking> bookkeys = getBookingHostryList(user);
        bookingList.remove(bookkeys.get(row));
    }
    public static List<Booking> getBookingList() {
        return bookingList;
    }

    public static void setBookingList(List<Booking> bookingList) {
        Booking.bookingList = bookingList;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCheckInPerson() {
        return checkInPerson;
    }

    public void setCheckInPerson(String checkInPerson) {
        this.checkInPerson = checkInPerson;
    }

    public String getCheckInPersonId() {
        return checkInPersonId;
    }

    public void setCheckInPersonId(String checkInPersonId) {
        this.checkInPersonId = checkInPersonId;
    }

    public LocalDate getCheckInTime() {
        return checkInTime;
    }

    public void setCheckInTime(LocalDate checkInTime) {
        this.checkInTime = checkInTime;
    }

    public LocalDate getCheckOutTime() {
        return checkOutTime;
    }

    public void setCheckOutTime(LocalDate checkOutTime) {
        this.checkOutTime = checkOutTime;
    }

    public RoomTypes getRoomTypes() {
        return roomTypes;
    }

    public void setRoomTypes(RoomTypes roomTypes) {
        this.roomTypes = roomTypes;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    @Override
    public String toString() {
        return "Booking{" +
                "user=" + user +
                ", checkInPerson='" + checkInPerson + '\'' +
                ", checkInPersonId='" + checkInPersonId + '\'' +
                ", checkInTime=" + checkInTime +
                ", checkOutTime=" + checkOutTime +
                ", roomTypes=" + roomTypes +
                ", price=" + price +
                ", status='" + status + '\'' +
                '}';
    }
}
